import Vue from 'vue'
import VueRouter from 'vue-router'
import systemRoutes from './modules/system'
import {getAccessToken} from "@/lib/tokenUtil";
import {setToken} from "@/lib/tokenUtil";

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes:[...systemRoutes]
})

/*设置全局前置导航守卫:
* 1.若当前已有token且跳转到登录首页是，重新导航到home
* 2.若当前没有token且想跳到首页以外的地址，重新导航到登录首页*/
router.beforeEach((to,from,next) => {
  const name = to.name;
  const token = getAccessToken();
  if(!!token && name == "login" ){
    next({name:"home"})
  }else if(!!!token && name != "login"){
    next({name:"login"})
  }else{
    //前往指定的目的地
    next();
  }
})
export default router
